package com.smile.logger.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Desc:接受模拟器生成的数据，并处理数据
 */

@RestController
@Slf4j
public class LoggerCtroller {

    //Spring提供对kafka的支持
    @Autowired  //将kafkaTemplate注入到Controller
    KafkaTemplate kafkaTemplate;

    //mock.url=http://localhost:8080/applog

    @RequestMapping("/applog")
    public String applog(@RequestBody  String mocklog){
        //数据落盘
        log.info(mocklog);
        //根据日志的不同类型，发送到kafka的不同主题
        //将接受到的字符串转换为json对象
        JSONObject jsonObject = JSON.parseObject(mocklog);
        JSONObject jsonObject1 = jsonObject.getJSONObject("start");
        if(jsonObject1 != null){
            //启动日志
            kafkaTemplate.send("smile_start_bak",mocklog);
        }else{
            //事件日志
            kafkaTemplate.send("smile_event_bak",mocklog);
        }


        return "success";

    }
}
